package com.hzqdn.monsterpost.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzqdn.monstercommon.annotation.AuthCheck;
import com.hzqdn.monstercommon.common.BaseResponse;
import com.hzqdn.monstercommon.common.DeleteRequest;
import com.hzqdn.monstercommon.common.ErrorCode;
import com.hzqdn.monstercommon.common.ResultUtils;
import com.hzqdn.monstercommon.exception.BusinessException;
import com.hzqdn.monsterfeign.UserFeignClient;
import com.hzqdn.monstermodel.dto.postComment.CommentAddRequest;
import com.hzqdn.monstermodel.dto.postComment.CommentQueryRequest;
import com.hzqdn.monstermodel.dto.postComment.CommentThumbAddRequest;
import com.hzqdn.monstermodel.dto.postComment.CommentUpdateRequest;
import com.hzqdn.monstermodel.entity.User;
import com.hzqdn.monstermodel.vo.CommentVO;
import com.hzqdn.monsterpost.service.CommentThumbService;
import com.hzqdn.monsterpost.service.PostCommentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 评论接口
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private PostCommentService postCommentService;

    @Resource
    private CommentThumbService commentThumbService;

    @Resource
    private UserFeignClient userFeignClient;

    /**
     * 创建评论
     */
    @PostMapping("/add")
    public BaseResponse<Long> addComment(@RequestBody CommentAddRequest commentAddRequest, HttpServletRequest request) {
        if (commentAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userFeignClient.getLoginUser(request);
        return ResultUtils.success(postCommentService.addComment(commentAddRequest, loginUser));
    }

    /**
     * 删除评论
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteComment(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userFeignClient.getLoginUser(request);
        long id = deleteRequest.getId();
        return ResultUtils.success(postCommentService.deleteComment(id, loginUser));
    }

    /**
     * 更新评论
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateComment(@RequestBody CommentUpdateRequest commentUpdateRequest, HttpServletRequest request) {
        if (commentUpdateRequest == null || commentUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        User loginUser = userFeignClient.getLoginUser(request);
        return ResultUtils.success(postCommentService.updateComment(commentUpdateRequest, loginUser));
    }

    /**
     * 根据ID获取评论
     */
    @GetMapping("/get")
    public BaseResponse<CommentVO> getCommentById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        User loginUser = userFeignClient.getLoginUserPermitNull(request);
        CommentVO commentVO = postCommentService.getCommentVO(postCommentService.getById(id), loginUser);
        return ResultUtils.success(commentVO);
    }

    /**
     * 分页获取评论列表
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<CommentVO>> listCommentByPage(@RequestBody CommentQueryRequest commentQueryRequest, HttpServletRequest request) {
        if (commentQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        User loginUser = userFeignClient.getLoginUserPermitNull(request);
        Page<CommentVO> commentVOPage = postCommentService.listCommentVOByPage(commentQueryRequest, loginUser);
        return ResultUtils.success(commentVOPage);
    }

    /**
     * 评论点赞
     */
    @PostMapping("/thumb")
    public BaseResponse<Integer> thumbComment(@RequestBody CommentThumbAddRequest commentThumbAddRequest, HttpServletRequest request) {
        if (commentThumbAddRequest == null || commentThumbAddRequest.getCommentId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userFeignClient.getLoginUser(request);
        return ResultUtils.success(commentThumbService.doThumb(commentThumbAddRequest.getCommentId(), loginUser));
    }

    /**
     * 管理员删除评论
     */
    @PostMapping("/delete/admin")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> deleteCommentAdmin(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        return ResultUtils.success(postCommentService.removeById(deleteRequest.getId()));
    }

    /**
     * 获取我的评论列表
     */
    @PostMapping("/my/list/page")
    public BaseResponse<Page<CommentVO>> listMyCommentByPage(@RequestBody CommentQueryRequest commentQueryRequest, HttpServletRequest request) {
        if (commentQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        User loginUser = userFeignClient.getLoginUser(request);
        commentQueryRequest.setUserId(loginUser.getId());
        Page<CommentVO> commentVOPage = postCommentService.listCommentVOByPage(commentQueryRequest, loginUser);
        return ResultUtils.success(commentVOPage);
    }
}
